import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/

export default defineConfig(({ mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd())

  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    server: {
      port: 8090,
      host: '0.0.0.0',
      proxy: {
        '/api': {
          target: env.VITE_API_URL, // 从环境变量中获取API URL
          changeOrigin: true, // 修改请求头中的 Origin
          rewrite: (path) => {
            // console.log('path', path, path.replace(/^\/api/, ''));
            return path.replace(/^\/api/, '')
          }, // 重写路径
        },
      },
    },
  }
})
